import Vue from 'vue'

function msg(title, duration = 1500, mask = false) {
	return new Promise((resolve) => {
		uni.showToast({
			title: title,
			icon: 'none',
			duration: duration,
			mask: mask,
			success: () => {
				setTimeout(() => {
					resolve('done')
				}, duration)
			}
		});
	})
}

const modal = function(options) {
	let {
		title,
		content,
		confirm,
		cancel
	} = options
	uni.showModal({
		title: title || '提示',
		content: content || '内容',
		success: res => {
			if (res.confirm) {
				if (confirm) {
					confirm();
				} else {
					console.log('确定')
				}
			} else if (res.cancel) {
				if (cancel) {
					cancel();
				} else {
					console.log('取消')
				}
			}
		}
	})
}

function goto(url = '', type = 'to') {
  if (typeof url === 'number') {
    uni.navigateBack({
      delta: url
    });
  } else if (typeof url === 'string') {
    if (url == '') {
      msg('暂未开放，敬请期待');
    }
    if (type == 'to') {
      uni.navigateTo({
        url: url
      });
    }
    if (type == 'tab') {
      uni.switchTab({
        url: url
      });
    }
    if (type == 'redirect') {
      uni.redirectTo({
        url: url
      });
    }
    if (type == 'relaunch') {
      uni.reLaunch({
        url: url
      });
    }
  }
}

const install = Vue => {
  Vue.prototype.$msg = msg
	Vue.prototype.$modal = modal
  Vue.prototype.$goto = goto
}

export default {
  install
}